package com.example.demo.pojo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * <h3>reqister</h3>
 * <p>用户实体类</p>
 *
 * @author : chenjunjie
 * @date : 2021-06-21 15:54
 **/
@Getter
@Setter
@ToString
public class Account implements RowMapper<Account>,UserDetails {
    private String id;
    private String username;
    private String password;
    private List<GrantedAuthority> authorities;
    private Integer roleId;
    private Integer status;
    private String realName;
    private boolean enabled=true;


    @Override
    public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
        Account account =new Account();

        account.setUsername(rs.getString("account_name"));
        account.setPassword(new BCryptPasswordEncoder().encode(rs.getString("account_password")));
        account.setRoleId(rs.getInt("role_id"));
        account.setStatus(rs.getInt("status"));
        account.setRealName(rs.getString("account_realname"));
        account.setId(rs.getString("account_id"));
        return account;
    }



    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }


    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }



}
